package com.wingice.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wingice.system.entity.SysDictionary;
import com.wingice.system.entity.SysDictionaryItem;
import com.wingice.system.exception.SysBusinessCode;
import com.wingice.system.exception.SysBusinessException;
import com.wingice.system.mapper.SysDictionaryItemMapper;
import com.wingice.system.service.SysDictionaryItemService;
import com.wingice.system.service.SysDictionaryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Transactional(rollbackFor = Exception.class)
@Service
public class SysDictionaryItemServiceImpl extends ServiceImpl<SysDictionaryItemMapper, SysDictionaryItem> implements SysDictionaryItemService {

    @Resource
    private SysDictionaryService sysDictionaryService;

    @Override
    public Boolean insert(SysDictionaryItem sysDictionaryItem) {
        final SysDictionary sysDictionary = sysDictionaryService.getById(sysDictionaryItem.getDictionaryId());
        if (sysDictionary == null) {
            throw new SysBusinessException(SysBusinessCode.SYS_DICTIONARY_ID_NOT_EXIST);
        }
        return save(sysDictionaryItem);
    }

    @Override
    public Boolean update(SysDictionaryItem sysDictionaryItem) {
        if (!StringUtils.isEmpty(sysDictionaryItem.getDictionaryId())) {
            final SysDictionary sysDictionary = sysDictionaryService.getById(sysDictionaryItem.getDictionaryId());
            if (sysDictionary == null) {
                throw new SysBusinessException(SysBusinessCode.SYS_DICTIONARY_ID_NOT_EXIST);
            }
        }
        return updateById(sysDictionaryItem);
    }
}
